package com.ruoyi.puyu.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.RoleEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.puyu.domain.PuyuMemberIntegral;
import com.ruoyi.puyu.mapper.PuyuMemberIntegralMapper;
import com.ruoyi.puyu.service.IPuyuMemberIntegralService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 队员积分Service业务层处理
 * 
 * @author wusongwen
 * @date 2024-12-18
 */
@Service
public class PuyuMemberIntegralServiceImpl implements IPuyuMemberIntegralService 
{
    @Autowired
    private PuyuMemberIntegralMapper puyuMemberIntegralMapper;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询队员积分
     * 
     * @param memberId 队员积分主键
     * @return 队员积分
     */
    @Override
    public PuyuMemberIntegral selectPuyuMemberIntegralByMemberId(Long memberId)
    {
        PuyuMemberIntegral puyuMemberIntegral = puyuMemberIntegralMapper.selectPuyuMemberIntegralByMemberId(memberId);
        if (null != puyuMemberIntegral) {
            String memberId1 = puyuMemberIntegral.getMemberId() + "_" + puyuMemberIntegral.getMemberName();
            Long rank = redisCache.getMemberRank(memberId1);
            rank = rank == null ? 0 : rank + 1;
            puyuMemberIntegral.setRank(rank);
        }
        return puyuMemberIntegral;
    }

    @Override
    public PuyuMemberIntegral selectPuyuMemberIntegralByPhoneAndName(PuyuMemberIntegral puyuMemberIntegral) {
        PuyuMemberIntegral puyuMemberIntegral1 = puyuMemberIntegralMapper.selectPuyuMemberIntegralByPhoneAndName(puyuMemberIntegral);
        if (null != puyuMemberIntegral1) {
            String memberId = puyuMemberIntegral1.getMemberId() + "_" + puyuMemberIntegral1.getMemberName();
            Long rank = redisCache.getMemberRank(memberId);
            rank = rank == null ? 0 : rank + 1;
            puyuMemberIntegral1.setRank(rank);
        }

        return puyuMemberIntegral1;
    }

    /**
     * 查询队员积分列表
     * 
     * @param puyuMemberIntegral 队员积分
     * @return 队员积分
     */
    @Override
    public List<PuyuMemberIntegral> selectPuyuMemberIntegralList(PuyuMemberIntegral puyuMemberIntegral)
    {
        return puyuMemberIntegralMapper.selectPuyuMemberIntegralList(puyuMemberIntegral);
    }

    @Override
    public List<PuyuMemberIntegral> selectPuyuMemberIntegralSort(PuyuMemberIntegral puyuMemberIntegral) {
        return puyuMemberIntegralMapper.selectPuyuMemberIntegralSort(puyuMemberIntegral);
    }

    /**
     * 新增队员积分
     * 
     * @param puyuMemberIntegral 队员积分
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPuyuMemberIntegral(PuyuMemberIntegral puyuMemberIntegral)
    {
        // 校验手机号
        PuyuMemberIntegral puyuMemberIntegralByPhone = puyuMemberIntegralMapper.selectPuyuMemberIntegralByPhoneAndName(puyuMemberIntegral);
        if (null != puyuMemberIntegralByPhone) {
            throw new ServiceException(MessageUtils.message("member.already.exists"));
        }

        SysUser user = userService.selectUserByPhoneNumber(puyuMemberIntegral.getPhone());
        if (null == user) {
            puyuMemberIntegral.setDeptId(103L);
            puyuMemberIntegral.setMemberType(2);
            user = new SysUser();
            user.setCreateBy(SecurityUtils.getUsername());
            String password = configService.selectConfigByKey("sys.user.initPassword");
            user.setPassword(SecurityUtils.encryptPassword(password));
            user.setDeptId(puyuMemberIntegral.getDeptId());
            user.setUserName(puyuMemberIntegral.getPhone());
            user.setNickName(puyuMemberIntegral.getMemberName());
            user.setPhonenumber(puyuMemberIntegral.getPhone());
            user.setEmail(puyuMemberIntegral.getPhone() + "@ptba.cn");
            user.setSex("0");
            user.setStatus("0");
            user.setDelFlag("0");
            Long[] roleIds = new Long[]{RoleEnum.OUTSIDE.getRoleId()};
            user.setRoleIds(roleIds);
            userService.insertUser(user);
        } else {
            puyuMemberIntegral.setMemberType(1);
        }
        if (null != puyuMemberIntegral.getIntegralBasic()) {
            puyuMemberIntegral.setIntegralTotal(puyuMemberIntegral.getIntegralBasic());
        }
        puyuMemberIntegral.setUserId(user.getUserId());
        puyuMemberIntegral.setDeptId(user.getDeptId());
        puyuMemberIntegral.setCreateBy(SecurityUtils.getUsername());
        puyuMemberIntegral.setCreateTime(DateUtils.getNowDate());
        int result = puyuMemberIntegralMapper.insertPuyuMemberIntegral(puyuMemberIntegral);

        // 写入缓存
        if (result > 0) {
            String memberId = puyuMemberIntegral.getMemberId() + "_" + puyuMemberIntegral.getMemberName();
            redisCache.addMemberIntegral(memberId, puyuMemberIntegral.getIntegralTotal());
        }
        return result;
    }

    @Override
    public int savePuyuMemberIntegral(PuyuMemberIntegral puyuMemberIntegral) {
        if (null != puyuMemberIntegral.getIntegralBasic()) {
            puyuMemberIntegral.setIntegralTotal(puyuMemberIntegral.getIntegralBasic());
        }
        puyuMemberIntegral.setMemberType(1);
        puyuMemberIntegral.setCreateBy(SecurityUtils.getUsername());
        puyuMemberIntegral.setCreateTime(DateUtils.getNowDate());
        int result = puyuMemberIntegralMapper.insertPuyuMemberIntegral(puyuMemberIntegral);

        // 写入缓存
        if (result > 0) {
            String memberId = puyuMemberIntegral.getMemberId() + "_" + puyuMemberIntegral.getMemberName();
            redisCache.addMemberIntegral(memberId, puyuMemberIntegral.getIntegralTotal());
        }
        return result;
    }

    /**
     * 修改队员积分
     * 
     * @param puyuMemberIntegral 队员积分
     * @return 结果
     */
    @Override
    public int updatePuyuMemberIntegral(PuyuMemberIntegral puyuMemberIntegral)
    {
        PuyuMemberIntegral puyuMemberIntegralById = puyuMemberIntegralMapper.selectPuyuMemberIntegralByMemberId(puyuMemberIntegral.getMemberId());

        if (null != puyuMemberIntegral.getIntegralBasic()) {
            puyuMemberIntegral.setIntegralTotal(puyuMemberIntegral.getIntegralBasic() + puyuMemberIntegralById.getIntegralCalculate());
        } else {
            puyuMemberIntegral.setIntegralTotal(puyuMemberIntegralById.getIntegralCalculate());
        }

        puyuMemberIntegral.setUpdateTime(DateUtils.getNowDate());

        int result = puyuMemberIntegralMapper.updatePuyuMemberIntegral(puyuMemberIntegral);
        // 写入缓存
        if (result > 0) {
            String memberId = puyuMemberIntegral.getMemberId() + "_" + puyuMemberIntegral.getMemberName();
            redisCache.addMemberIntegral(memberId, puyuMemberIntegral.getIntegralTotal());
        }
        return result;
    }

    @Override
    public int updatePuyuMemberIntegralPhone(PuyuMemberIntegral puyuMemberIntegral) {
        return puyuMemberIntegralMapper.updatePuyuMemberIntegralPhone(puyuMemberIntegral);
    }

    /**
     * 批量删除队员积分
     * 
     * @param memberIds 需要删除的队员积分主键
     * @return 结果
     */
    @Override
    public int deletePuyuMemberIntegralByMemberIds(Long[] memberIds)
    {
        return puyuMemberIntegralMapper.updatePuyuMemberIntegralByMemberIds(memberIds);
    }

    /**
     * 删除队员积分信息
     * 
     * @param memberId 队员积分主键
     * @return 结果
     */
    @Override
    public int deletePuyuMemberIntegralByMemberId(Long memberId)
    {
        return puyuMemberIntegralMapper.deletePuyuMemberIntegralByMemberId(memberId);
    }
}
